package us.kg.kotoriforest.senior;

import org.junit.Assert;
import org.junit.Test;
import us.kg.kotoriforest.senior.dao.BankDAO;
import us.kg.kotoriforest.senior.dao.EmployeeDAO;
import us.kg.kotoriforest.senior.dao.impl.BankDAOImpl;
import us.kg.kotoriforest.senior.dao.impl.EmployeeDAOImpl;
import us.kg.kotoriforest.senior.pojo.Employee;
import us.kg.kotoriforest.senior.util.JDBCUtil;
import us.kg.kotoriforest.senior.util.JDBCUtilVer2;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * ClassName: us.kg.kotoriforest.senior.JDBCUtilTest </br>
 * Description: 测试 JDBCUtil 类
 *
 * @author Kotori rotroutine@163.com
 * @since 2024/11/28
 */
public class JDBCUtilTest {
    @Test
    public void testUtilVer1() throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        System.out.println(connection);
        JDBCUtil.close(connection);
    }

    @Test
    public void testUtilVer2() throws SQLException {
        Connection connection1 = JDBCUtilVer2.getConnection();
        Connection connection2 = JDBCUtilVer2.getConnection();
        Connection connection3 = JDBCUtilVer2.getConnection();
        Connection connection4 = JDBCUtilVer2.getConnection();
        Connection connection5 = JDBCUtilVer2.getConnection();

        // 都是同一个对象
        Assert.assertSame(connection1, connection2);
        System.out.println(connection1);
        System.out.println(connection2);
        System.out.println(connection3);
        System.out.println(connection4);
        System.out.println(connection5);

        JDBCUtilVer2.close();
    }

    @Test
    public void testUtilVer1Again() throws SQLException {
        Connection connection1 = JDBCUtil.getConnection();
        Connection connection2 = JDBCUtil.getConnection();
        Connection connection3 = JDBCUtil.getConnection();
        Connection connection4 = JDBCUtil.getConnection();
        Connection connection5 = JDBCUtil.getConnection();

        // 会产生 5 个不同的对象
        System.out.println(connection1);
        System.out.println(connection2);
        System.out.println(connection3);
        System.out.println(connection4);
        System.out.println(connection5);

        JDBCUtilVer2.close();
    }

    @Test
    public void testDA1() {
        EmployeeDAO employeeDAO = new EmployeeDAOImpl();
        List<Employee> employees = employeeDAO.selectAll();
        employees.forEach(System.out::println);
    }

    @Test
    public void testDAO2() {
        EmployeeDAO employeeDAO = new EmployeeDAOImpl();
        Employee employee = employeeDAO.selectByEmpId(2);
        System.out.println(employee);
    }

    @Test
    public void test3() {
        EmployeeDAO employeeDAO = new EmployeeDAOImpl();
        Employee employee = new Employee(null, "小明", 520.13, 14);
        int row = employeeDAO.insert(employee);
        System.out.println(row);
    }

    @Test
    public void test4() {
        EmployeeDAO employeeDAO = new EmployeeDAOImpl();
        Employee employee = employeeDAO.selectByEmpId(10008);
        if (employee != null) {
            employee.setEmpSalary(employee.getEmpSalary() * 0.8);
            int update = employeeDAO.update(employee);
            System.out.println(update);
        }
    }

    @Test
    public void test5() {
        EmployeeDAO employeeDAO = new EmployeeDAOImpl();
        int delete = employeeDAO.delete(10008);
        System.out.println(delete);
    }

    @Test
    public void test6() {
        Connection connection = null;
        try {
            connection = JDBCUtilVer2.getConnection();
            connection.setAutoCommit(false); // 开启事务

            BankDAO bankDAO = new BankDAOImpl();
            bankDAO.subMoney(1, 100);

            int i = 10 / 0;
            bankDAO.addMoney(2, 100);

            connection.commit();
        } catch (Exception e) {
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        } finally {
            JDBCUtilVer2.close();
        }

    }
}
